#include "WaveForm.h"
#include "ui_WaveForm.h"
#include <QDateTime>

WaveForm::WaveForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WaveForm)
{
    ui->setupUi(this);
    this->setWindowTitle("折线图显示");

    ui->widget_Cur->setOpenGl(true); // 必须调用前检查OpenGL支持
    ui->widget_Vol->setOpenGl(true); // 必须调用前检查OpenGL支持

    ui->widget_Cur->addGraph();         // 增加图层
//    ui->widget->graph(0)->setPen(QPen(Qt::red));
    ui->widget_Cur->graph(0)->setAntialiasedFill(false);        // 设置图层反锯齿：关闭

    ui->widget_Cur->graph(0)->setPen(QPen(Qt::blue,2));         // 设置笔的颜色
    ui->widget_Cur->graph(0)->setLineStyle(QCPGraph::lsLine);   // 线条

    ui->widget_Cur->addGraph();
    ui->widget_Cur->graph(1)->setPen(QPen(Qt::red,0.1));       // 设置笔的颜色
    ui->widget_Cur->graph(1)->setLineStyle(QCPGraph::lsNone);   // 不画线条
    ui->widget_Cur->graph(1)->setScatterStyle(QCPScatterStyle::ssDisc);     // 设置点的形状


    QSharedPointer<QCPAxisTickerDateTime> timeTicker(new QCPAxisTickerDateTime);
    timeTicker->setDateTimeFormat("hh:mm:ss");

    //    timeTicker->setTimeFormat("yyyy-MM-dd hh:mm:ss"); // 日期和时间换行显示，防止过长，显示不下
    ui->widget_Cur->xAxis->setTicker(timeTicker);
    ui->widget_Cur->xAxis->setTickLabels(true);
    ui->widget_Cur->axisRect()->setupFullAxesBox();

    // 设置y轴
    ui->widget_Cur->yAxis->setRange(-1,11);
    ui->widget_Cur->xAxis->setLabel("时间");
    ui->widget_Cur->yAxis->setLabel("电流");

    ui->widget_Cur->axisRect()->setRangeDrag(Qt::Vertical | Qt::Horizontal);//设置曲线水平方向拖动
    ui->widget_Cur->axisRect()->setRangeZoom(Qt::Vertical | Qt::Horizontal);


//    ui->widget->axisRect()->setRangeZoomFactor(2);
//    ui->widget->axisRect()->setRangeZoom(Qt::Vertical | Qt::Horizontal);

    // 轴随动
    connect(ui->widget_Cur->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->widget_Cur->xAxis2, SLOT(setRange(QCPRange)));
    connect(ui->widget_Cur->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->widget_Cur->yAxis2, SLOT(setRange(QCPRange)));


    ui->widget_Vol->addGraph();         // 增加图层
//    ui->widget->graph(0)->setPen(QPen(Qt::red));
    ui->widget_Vol->graph(0)->setAntialiasedFill(false);        // 设置图层反锯齿：关闭

    ui->widget_Vol->graph(0)->setPen(QPen(Qt::blue,2));         // 设置笔的颜色
    ui->widget_Vol->graph(0)->setLineStyle(QCPGraph::lsLine);   // 线条


    ui->widget_Vol->addGraph();
    ui->widget_Vol->graph(1)->setPen(QPen(Qt::red,0.1));       // 设置笔的颜色
    ui->widget_Vol->graph(1)->setLineStyle(QCPGraph::lsNone);   // 不画线条
    ui->widget_Vol->graph(1)->setScatterStyle(QCPScatterStyle::ssDisc);     // 设置点的形状


    //QSharedPointer<QCPAxisTickerDateTime> timeTicker(new QCPAxisTickerDateTime);
    //timeTicker->setDateTimeFormat("hh:mm:ss");

    //    timeTicker->setTimeFormat("yyyy-MM-dd hh:mm:ss"); // 日期和时间换行显示，防止过长，显示不下
    ui->widget_Vol->xAxis->setTicker(timeTicker);
    ui->widget_Vol->xAxis->setTickLabels(true);
    ui->widget_Vol->axisRect()->setupFullAxesBox();

    // 设置y轴
    ui->widget_Vol->yAxis->setRange(-1,11);
    ui->widget_Vol->xAxis->setLabel("时间");
    ui->widget_Vol->yAxis->setLabel("电压");

    ui->widget_Vol->axisRect()->setRangeDrag(Qt::Vertical | Qt::Horizontal);//设置曲线水平方向拖动
    ui->widget_Vol->axisRect()->setRangeZoom(Qt::Vertical | Qt::Horizontal);


//    ui->widget->axisRect()->setRangeZoomFactor(2);
//    ui->widget->axisRect()->setRangeZoom(Qt::Vertical | Qt::Horizontal);

    // 轴随动
    connect(ui->widget_Vol->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->widget_Vol->xAxis2, SLOT(setRange(QCPRange)));
    connect(ui->widget_Vol->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->widget_Vol->yAxis2, SLOT(setRange(QCPRange)));


}

WaveForm::~WaveForm()
{
    delete ui;
}

void WaveForm::setCurrentValue(float fCurrent)
{
    quint64 mSecs = QDateTime::currentMSecsSinceEpoch();
    double dkey = static_cast<double>(mSecs) / 1000.0;
    ui->widget_Cur->graph(0)->addData(dkey, fCurrent);
    ui->widget_Cur->graph(1)->addData(dkey, fCurrent);
    ui->widget_Cur->xAxis->setRange(dkey,100, Qt::AlignRight);
    ui->widget_Cur->replot();
}

void WaveForm::setVoltageValue(float fVoltage)
{
    quint64 mSecs = QDateTime::currentMSecsSinceEpoch();
    double dkey = static_cast<double>(mSecs) / 1000.0;
    ui->widget_Vol->graph(0)->addData(dkey, fVoltage);
    ui->widget_Vol->graph(1)->addData(dkey, fVoltage);
    ui->widget_Vol->xAxis->setRange(dkey,100, Qt::AlignRight);
    ui->widget_Vol->replot();
}

void WaveForm::setCurVolValue(float fCurrent, float fVoltage)
{
    quint64 mSecs = QDateTime::currentMSecsSinceEpoch();
    double dkey = static_cast<double>(mSecs) / 1000.0;
    ui->widget_Cur->graph(0)->addData(dkey, fCurrent);
    ui->widget_Cur->graph(1)->addData(dkey, fCurrent);
    ui->widget_Cur->xAxis->setRange(dkey,100, Qt::AlignRight);
    ui->widget_Cur->replot();
    ui->widget_Vol->graph(0)->addData(dkey, fVoltage);
    ui->widget_Vol->graph(1)->addData(dkey, fVoltage);
    ui->widget_Vol->xAxis->setRange(dkey,100, Qt::AlignRight);
    ui->widget_Vol->replot();


    // 删除图层0中key小于某个值的数据
    ui->widget_Cur->graph(0)->data()->removeBefore(dkey-240.0);
    ui->widget_Cur->graph(1)->data()->removeBefore(dkey-240.0);
    ui->widget_Vol->graph(0)->data()->removeBefore(dkey-240.0);
    ui->widget_Vol->graph(1)->data()->removeBefore(dkey-240.0);
//    // 删除图层0中key大于某个值的数据
//    ui->widget->graph(0)->data()->removeDataAfter(某个键值);


//    // 删除图层0中key在某个范围内的数据
//    ui->widget->graph(0)->data()->removeData(开始键值, 结束键值);



}

void WaveForm::closeEvent(QCloseEvent *e)
{
    emit closeForm();
    Q_UNUSED(e);
}
